package com.bgds.sys.rbac.controller;

import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.rbac.dao.PermissionDao;
import com.bgds.sys.rbac.domain.Permission;

import com.bgds.sys.rbac.service.PermissionService;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;

/**
 * @author lj
 */
@Slf4j
@RestController
@Api(description = "菜单/权限管理接口")
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private PermissionDao permissionDao;



    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加菜单",notes = "添加菜单",httpMethod = "POST")
    public Result<Permission> add(@ModelAttribute Permission permission){
        Permission p = permissionService.insert(permission);

        return new ResultUtil<Permission>().setData(p);
    }


    /**
     * 查询所有菜单
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "查询所有菜单",notes = "查询所有菜单",httpMethod = "GET",response = Result.class)
    public Result<List<Permission>> getPermissionAll(){
        List<Permission> list=permissionDao.findAll();
        list.sort(Comparator.comparing(Permission::getSortOrder));
        return new ResultUtil<List<Permission>>().setData(list);
    }


}
